//注意概率分布不是以面积来分的，而是以整数点的个数来分的，需要(x+1)*(y+1)而不是x*y
import java.util.Arrays;

public class Solution497 {

    int[][] rec;
    int[] size;
    double[] pace;

    public Solution497(int[][] rects) {
        size=new int[rects.length];
        rec=rects;
        for (int i=0;i<rects.length;i++){
            int x=rects[i][2]-rects[i][0]+1;
            int y=rects[i][3]-rects[i][1]+1;
            size[i]=x*y;
        }
        pace=new double[rects.length+1];
        int total=Arrays.stream(size).sum();
        int pre=0;
        for (int i=0;i<rects.length;i++){
            pre+=size[i];
            pace[i+1]=1.0*pre/total;
        }
    }

    public int[] pick() {
        int t=0;
        double m=Math.random();
        for (int i=0;i<pace.length;i++){
            if (m>pace[i] && m<pace[i+1]){
                t=i;
                break;
            }
        }
        int x=rec[t][2]-rec[t][0]+1;
        int y=rec[t][3]-rec[t][1]+1;
        return new int[]{(int)(Math.random()*x)+rec[t][0],(int)(Math.random()*y)+rec[t][1]};
    }

    public static void main(String[] args) {
        Solution497 solution497=new Solution497(new int[][]{{-2,-2,1,1},{2,2,4,6}});
        System.out.println(Arrays.toString(solution497.pick()));
    }
}
